﻿#region Using

//using Forside.Contracts.MasterSystem.DataContracts;
using System;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using System.Text;

#endregion
namespace LangMauViet.Classes.Helper
{
    #region Enums
    #region ComboType
    public enum ComboType
    {
        ContactReason = 1
    }
    #endregion ComboType
    #region RecommendedContentDisplayType
    public enum RecommendedContentDisplayType
    {
        ProductImages = 1,
        ProductImage3Title = 2
    }
    #endregion RecommendedContentDisplayType
    #endregion Enums
    public struct LinkItem
    {
        public string Href;
        public string Text;
        public override string ToString()
        {
            return Href + "\n\t" + Text;
        }
    }
    
    public static class UIHelper
    {
        /// <summary>
        /// Gets the device ID.
        /// </summary>
        /// <returns></returns>
        public static int GetDeviceID()
        {
            int intDeviceID = 4; // DEFAULT FOR WEB
            string strUserAgent = HttpContext.Current.Request.UserAgent == null ? "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-us) AppleWebkit/533.16 (KHTML, like Gecko) Version/5.0 Safari/533.16" : HttpContext.Current.Request.UserAgent.ToString();
            // check if the request is from mobile device
            if (isMobileDevice())
            {
                intDeviceID = 5;
            }
            // for iPhone
            if (strUserAgent.IndexOf("iPhone", StringComparison.OrdinalIgnoreCase) >= 0)
            {
                intDeviceID = 3;
            }
            // for Android
            else if (strUserAgent.IndexOf("Android", StringComparison.OrdinalIgnoreCase) >= 0 ||
                     strUserAgent.IndexOf("Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-us) AppleWebkit/533.16 (KHTML, like Gecko) Version/5.0 Safari/533.16") >= 0 ||
                     strUserAgent.IndexOf("Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_7;en-us) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Safari/530.17") >= 0)
            {
                intDeviceID = 1;
            }
            // BlackBerry
            else if (strUserAgent.IndexOf("BlackBerry", StringComparison.OrdinalIgnoreCase) >= 0)
            {
                intDeviceID = 2;
            }
            //iPad
            else if (strUserAgent.IndexOf("iPad", StringComparison.OrdinalIgnoreCase) >= 0)
            {
                intDeviceID = 6;
            }
            else if ((strUserAgent.IndexOf("Windows Phone OS 7", StringComparison.OrdinalIgnoreCase) >= 0) ||
                     (strUserAgent.IndexOf("WP7", StringComparison.OrdinalIgnoreCase) >= 0) ||
                     (strUserAgent.IndexOf("Windows Phone", StringComparison.OrdinalIgnoreCase) >= 0))
            {
                intDeviceID = 7;
            }
            else if (strUserAgent.IndexOf("LG Browser", StringComparison.OrdinalIgnoreCase) >= 0 || strUserAgent.IndexOf("LG NetCast", StringComparison.OrdinalIgnoreCase) >= 0 || strUserAgent.IndexOf("SmartTV", StringComparison.OrdinalIgnoreCase) >= 0)
            {
                intDeviceID = 15;
            }
            //else if ((strUserAgent.IndexOf("SymbianOS", StringComparison.OrdinalIgnoreCase) >= 0))
            //{
            //    intDeviceID = 9;
            //}
            else
            {
                intDeviceID = 4;
            }
            return intDeviceID;
        }
        /// <summary>
        /// Determines whether [is mobile device].
        /// </summary>
        /// <returns>
        /// 	<c>true</c> if [is mobile device]; otherwise, <c>false</c>.
        /// </returns>                           
        public static Boolean isMobileDevice()
        {
            HttpContext curcontext = HttpContext.Current;
            string user_agent = curcontext.Request.ServerVariables["HTTP_USER_AGENT"];
            // Checks the user-agent
            if (user_agent != null)
            {
                user_agent = user_agent.ToLower();
                // Checks if its a Windows browser but not a Windows Mobile browser
                if (user_agent.Contains("windows") && !user_agent.Contains("windows ce"))
                {
                    return false;
                }
                // Checks if it is a mobile browser
                string pattern = "up.browser|up.link|windows ce|iphone|iemobile|mini|mmp|symbian|midp|wap|phone|pocket|mobile|pda|psp";
                MatchCollection mc = Regex.Matches(user_agent, pattern, RegexOptions.IgnoreCase);
                if (mc.Count > 0)
                    return true;
                // Checks if the 4 first chars of the user-agent match any of the most popular user-agents
                string popUA = "|acs-|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-|dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-|maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv|palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany|sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo|teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3c |wap-|wapa|wapi|wapp|wapr|webc|winw|winw|xda|xda-|";
                if (popUA.Contains("|" + user_agent.Substring(0, 4) + "|"))
                    return true;
            }
            // Checks the accept header for wap.wml or wap.xhtml support
            string accept = curcontext.Request.ServerVariables["HTTP_ACCEPT"];
            if (accept != null)
            {
                if (accept.Contains("text/vnd.wap.wml") || accept.Contains("application/vnd.wap.xhtml+xml"))
                {
                    return true;
                }
            }
            // Checks if it has any mobile HTTP headers
            string x_wap_profile = curcontext.Request.ServerVariables["HTTP_X_WAP_PROFILE"];
            string profile = curcontext.Request.ServerVariables["HTTP_PROFILE"];
            string opera = curcontext.Request.Headers["HTTP_X_OPERAMINI_PHONE_UA"];
            if (x_wap_profile != null || profile != null || opera != null)
            {
                return true;
            }
            return false;
        }

        public static MvcHtmlString Paging(this HtmlHelper htmlHelper, int currentPage, int recordPerPage, int totalRecord, int type)
        {
            string htmlPaging = string.Empty;

            int pageCount = totalRecord % recordPerPage == 0
                                ? totalRecord / recordPerPage
                                : totalRecord / recordPerPage + 1;
            
            htmlPaging = "<ul class='home-nav'>";
            /*if (totalRecord > -1)
            {
                htmlPaging += "<li class='left'>Có : " + totalRecord + " sản phẩm </li>";
            }*/
            if (currentPage > 5)
            {
                int page = currentPage%5 == 0 ? (currentPage/5 - 1)*5 : (currentPage/5)*5;
                htmlPaging +="<li><a href='javascript:void(0);' onclick='(PageClick("+page+","+type+"))'>Trước</a></li>";

            }
           
            string pageNumber = string.Empty;
            if (pageCount > 5 && currentPage > 5)
            {
                if (pageCount >= currentPage+5)
                {
                    for (int i = currentPage - 4; i <= currentPage; i++)
                    {
                        pageNumber += currentPage == i ? "<li class='active'>" + i + "</li>"
                                                : "<li><a href='javascript:void(0);' onclick='(PageClick(" + i + "," + type + "))'>" + i + "</a></li>";
                    }
                }
                else
                {
                    for (int i = pageCount - 4; i <= pageCount; i++)
                    {
                        pageNumber += currentPage == i ? "<li class='active'>" + i + "</li>"
                                                : "<li><a href='javascript:void(0);' onclick='(PageClick(" + i + "," + type + "))'>" + i + "</a></li>";
                    }
                }
            }
            else if (pageCount > 5 && currentPage <= 5)
            {
                for (int i = 1; i <= 5; i++)
                {
                    pageNumber += currentPage == i ? "<li class='active'>" + i + "</li>"
                                                : "<li><a href='javascript:void(0);' onclick='(PageClick(" + i + "," + type + "))'>" + i + "</a></li>";
                }
            }
            else
            {
                for (int i = 1; i <= pageCount; i++)
                {
                    pageNumber += currentPage == i ? "<li class='active'>" + i + "</li>"
                                                : "<li><a href='javascript:void(0);' onclick='(PageClick(" + i + "," + type + "))'>" + i + "</a></li>";
                }
            }
            htmlPaging += pageNumber;

            if (currentPage < pageCount)
            {
                htmlPaging += "<li><a href='javascript:void(0);' onclick='(PageClick(" + ((currentPage / 5) * 5 + 6) + "," + type + "))'>Tiếp</a></li>";
            }

            htmlPaging += "</ul>";

            return MvcHtmlString.Create(htmlPaging);
        }

        public static string convertToUnSign(string s)
        {
            s=s.Replace("\"", "-").Replace("'", "-").Replace("--","-");
            Regex regex = new Regex("\\p{IsCombiningDiacriticalMarks}+");
            string temp = s.Normalize(NormalizationForm.FormD);
            return regex.Replace(temp, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D').Replace(' ','-').Replace("--","-");
        }  
    }
}
